#include <pmon.h>
#include <linux/types.h>

void i2c_init(int speed,  int slaveaddr);
int i2c_read(u8 chip, uint addr, int alen, u8 *buffer, int len);
int i2c_write(u8 chip, uint addr, int alen, u8 *buffer, int len);

#define TCA6424_ADDR	0x22

int tca6424_init(void)
{
	u8 chip = TCA6424_ADDR;
	u8 reg,port,port_val;
	u8 buf[3]={0xff,0xff,0xff};
	int status;

	ls2k_i2c_init(0, 0xbfe01800);

	port = (TCA6424_BEEP & 0xF0) >> 4;           //高四位为port位
	port_val  = ~(1 << (TCA6424_BEEP & 0x0F));
	buf[port] = port_val;
	printf("tca6424 beep buf[%d]=0x%x \n", port,buf[port]);

	reg = 0x0c | 0x80;                            //1为输入，0为输出
	status = i2c_write(chip, reg, 1, &buf, 3);

	reg = 0x04 | 0x80;                            //蜂鸣器1有效  1为高电平，0为低电平
	buf[port] = 0xff;
	status = i2c_write(chip, reg, 1, &buf, 3);

	delay(50000);  //延时50ms

	reg = 0x04 | 0x80;
	buf[port] = port_val;
	status = i2c_write(chip, reg, 1, &buf, 3);

	printf("tca6424 init complete %d\n", status);
	return 0;
}

